package com.xiaohao.healthrecords.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaohao.healthrecords.entity.HealthRecord;
import com.xiaohao.healthrecords.mapper.HealthRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class HealthRecordService {
    @Autowired
    private HealthRecordMapper healthRecordMapper;

    public HealthRecord query(HealthRecord healthRecord) {
        LambdaQueryWrapper<HealthRecord> queryWrapper = new LambdaQueryWrapper<>(healthRecord);
        return healthRecordMapper.selectOne(queryWrapper);
    }

    public List<HealthRecord> queryList(HealthRecord healthRecord) {
        LambdaQueryWrapper<HealthRecord> queryWrapper = new LambdaQueryWrapper<>(healthRecord);
        return healthRecordMapper.selectList(queryWrapper);
    }

    public int update(HealthRecord healthRecord) {
        if (Objects.isNull(healthRecord)){
            return 0;
        }
        if (Objects.nonNull(healthRecord.getId())){
            return healthRecordMapper.updateById(healthRecord);
        }else {
            return healthRecordMapper.insert(healthRecord);
        }
    }

    public int delete(Integer id) {
        return healthRecordMapper.deleteById(id);
    }
}
